﻿using System;
using System.Data;
using System.Data.SQLite;

namespace ESpace.DALibrary.Sqlite
{
    public  class DataAccessBase : IDisposable
    {


        protected SQLiteParameter paramReturnValue;
        protected SQLiteCommand sqliteComm;

        private bool disposed = false;
        public void Dispose()
        {
            if (!disposed)
            {
                if (sqliteComm != null)
                {
                    if (sqliteComm.Connection != null)
                        sqliteComm.Connection.Dispose();

                    sqliteComm.Dispose();
                }
                disposed = true;
            }//113.90.60.146
        }

        public int ReturnValue
        {
            get
            {
                return (System.Int32)this.paramReturnValue.Value;
            }
        }

        /// <summary>
        /// 初始化SqlCommand命令
        /// </summary>
        /// <param name="sSPName">要初始化的存储过程名称</param>
        protected void InitCommand(string sSPName)
        {

            this.sqliteComm = new SQLiteCommand();
            this.sqliteComm.CommandText = sSPName;
            this.sqliteComm.CommandType = CommandType.Text;
            //--------------------------------------------------------
            //this.paramReturnValue = new SQLiteParameter();
            //this.paramReturnValue.ParameterName = "@ReturnValue";
            //this.paramReturnValue.DbType = DbType.Int32;
            //this.paramReturnValue.Size = 4;
            //this.paramReturnValue.Direction = ParameterDirection.ReturnValue;
            //this.sqliteComm.Parameters.Add(this.paramReturnValue);
            //--------------------------------------------------------
        }



        /// <summary>
        /// 执行查询命令，并返回DataTable对象
        /// </summary>
        /// <returns>执行结果</returns>
        public DataTable ExecuteDataTable()
        {
            return ExecuteDataTable("");
        }

        /// <summary>
        /// 执行查询命令，并返回DataTable对象
        /// </summary>
        /// <param name="sTableName">返回的DataTable名称</param>
        /// <returns>执行结果</returns>
        public DataTable ExecuteDataTable(string sTableName)
        {

            DataTable dtResult;
            using (SQLiteConnection sqliteConn = new SQLiteConnection(_strConn))
            {

                sqliteComm.Connection = sqliteConn;

                SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqliteComm);

                dtResult = new DataTable();

                adapter.Fill(dtResult);

                sqliteConn.Close();

                if (sTableName != "")
                {
                    dtResult.TableName = sTableName;
                }
            }
            return dtResult;
        }


        /// <summary>
        /// 执行查询命令，并返回DataSet对象
        /// </summary>
        /// <returns>执行结果</returns>
        public DataSet ExecuteDataSet()
        {
            DataSet dsResult;
            using (SQLiteConnection sqliteConn = new SQLiteConnection(_strConn))
            {
                sqliteComm.Connection = sqliteConn;
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqliteComm);

                dsResult = new DataSet();

                adapter.Fill(dsResult);
                sqliteConn.Close();
            }
            return dsResult;
        }



        /// <summary>
        /// 执行查询命令，并返回DataSet对象
        /// </summary>
        /// <param name="sTableName">返回的DataTable名称</param>
        /// <returns>执行结果</returns>
        public DataSet ExecuteDataSet(string sTableName)
        {
            DataSet dsResult;
            using (SQLiteConnection sqliteConn = new SQLiteConnection(_strConn))
            {
                sqliteComm.Connection = sqliteConn;
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqliteComm);

                dsResult = new DataSet(sTableName);

                adapter.Fill(dsResult);

                sqliteConn.Close();

            }
            return dsResult;
        }

        /// <summary>
        /// 执行查询命令
        /// </summary>
        /// <returns>影响记录数</returns>
        public int ExecuteNonQuery()
        {
            int iResult = -1;

            using (SQLiteConnection sqliteConn = new SQLiteConnection(_strConn))
            {
                sqliteConn.Open();
                sqliteComm.Connection = sqliteConn;
                iResult = sqliteComm.ExecuteNonQuery();
                sqliteConn.Close();
            }
            return iResult;
        }

        /// <summary>
        /// 执行查询命令，并返回执行结果的第一行第一列的值
        /// </summary>
        /// <returns>执行结果集第一行第一列的值</returns>
        public object ExecuteScalar()
        {
            object objReturn = null;
            using (SQLiteConnection sqliteConn = new SQLiteConnection(_strConn))
            {
                sqliteComm.Connection = sqliteConn;

                sqliteConn.Open();
                sqliteComm.Connection = sqliteConn;
                objReturn = sqliteComm.ExecuteScalar();
                sqliteConn.Close();
            }
            return objReturn;
        }


         /// <summary>  
        /// 查询数据库中的所有数据类型信息  
        /// </summary>  
        /// <returns></returns>  
        public DataTable GetSchema()  
        {
            using (SQLiteConnection connection = new SQLiteConnection(_strConn))  
            {  
                connection.Open();  
                DataTable data=connection.GetSchema("TABLES");  
                connection.Close();  
                return data;  
            }  
        }  



        private string _strConn;
        /// <summary>
        /// 获取或设置数据库连接串
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return _strConn;
            }
            set
            {
                _strConn = Decrypt(value);
            }
        }

        public string DBSection
        {
            set
            {
                _strConn = Decrypt(System.Configuration.ConfigurationManager.ConnectionStrings[value].ConnectionString);
            }
        }

        private string _Key;
        protected string Key
        {
            set
            {
                _Key = value;
            }
        }

        private string _IV;
        protected string IV
        {
            set
            {
                _IV = value;
            }
        }

        private Alogrithm.SymmProvEnum _EncryptMethod;
        protected Alogrithm.SymmProvEnum EncryptMethod
        {
            set
            {
                _EncryptMethod = value;
            }
        }

        private string Decrypt(string strSource)
        {
            if (_EncryptMethod == Alogrithm.SymmProvEnum.None)
            {
                return strSource;
            }
            else
            {
                Alogrithm objAlogrim = new Alogrithm(_EncryptMethod);
                objAlogrim.Key = _Key;
                objAlogrim.IV = _IV;

                return objAlogrim.Decrypting(strSource);
            }
        }
    }

}
